namespace Spotted.Storage.Tables.Pages
{
    using System;
    using System.Windows.Controls;
    using System.Windows.Controls.Primitives;
    using Spotted.Storage.Tables.ViewModels;

    public partial class SampleDataTablePage
    {
        public SampleDataTablePage()
        {
            InitializeComponent();
            this.ViewModel = new SampleDataTablePageViewModel();
        }

        public SampleDataTablePageViewModel ViewModel
        {
            get { return this.DataContext as SampleDataTablePageViewModel; }
            set { this.DataContext = value; }
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            this.ViewModel.LoadTable();
        }

        private void OnSampleDataSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selector = sender as Selector;
            if ((selector == null) || (selector.SelectedIndex == -1))
                return;

            ApplicationStateHelpers.Set("CurrentSampleDataRow", this.ViewModel.Table[selector.SelectedIndex]);
            this.NavigationService.Navigate(new Uri("/Storage/Tables/Pages/SampleDataDetailsPage.xaml?editSampleData=true", UriKind.Relative));

            selector.SelectedIndex = -1;
        }

        private void OnLoadTable(object sender, EventArgs e)
        {
            this.ViewModel.LoadTable();
        }

        private void OnAddRow(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Storage/Tables/Pages/SampleDataDetailsPage.xaml?editSampleData=false", UriKind.Relative));
        }
    }
}